/*
 * author      : 吴中勤 
 * date        : 2018/8/21
 * description : TODO
 */

package com.deepexi.adapter.sdk.sap;

import com.deepexi.adapter.common.CommonUtils;
import com.deepexi.adapter.sdk.sap.dto.bp.SapBPResponseBody;
import com.deepexi.adapter.sdk.sap.dto.fund.SapFundResponseBody;
import com.deepexi.adapter.sdk.sap.dto.order.SapOrderResponseBody;
import org.json.JSONException;
import org.json.JSONObject;

public class ResponseParser {

    /** 解析 BP请求的结果, 数据结构 请看 doc/sap_api/BP.md */
    public static SapBPResponseBody parseResult4BP(String result) throws JSONException {
        JSONObject records = getRecordsNode(result);
        return CommonUtils.jsonString2Obj(records.toString(), SapBPResponseBody.class);
    }

    /** 解析 SapAdapterOrder 请求的结果, 数据结构 请看 doc/sap_api/SapAdapterOrder.md */
    public static SapOrderResponseBody parseResult4Order(String result) throws JSONException {
        JSONObject records = getRecordsNode(result);
        return CommonUtils.jsonString2Obj(records.toString(), SapOrderResponseBody.class);
    }

    public static SapFundResponseBody parseResult4Fund(String result) {
        JSONObject records = getRecordsNode(result);
        return CommonUtils.jsonString2Obj(records.toString(), SapFundResponseBody.class);
    }

    /** 解析 callback 请求的结果 */
    public static String parseResult4Callback(String result) {
        return result;
    }

    private static JSONObject getRecordsNode(String result) throws JSONException {
        JSONObject json = new JSONObject(result);
        return (JSONObject) json.get(SapBizConst.RESPONSE_NODE_RECORDS);
    }


}
